//
//  ViewController.m
//  201-数据存储
//
//  Created by student on 16/3/14.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "ViewController.h"
#import "studentModel.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;

@property (weak, nonatomic) IBOutlet UITextField *ageTextField;

@property (weak, nonatomic) IBOutlet UITextField *idTextField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)save1:(id)sender {
    NSUserDefaults *stuDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = @{@"姓名":self.nameTextField.text,@"年龄":self.ageTextField.text,@"学号":self.idTextField.text};
    [stuDefaults setObject:dic forKey:@"学生信息"];
}
- (IBAction)read1:(id)sender {
    NSUserDefaults *stuDefaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic = [stuDefaults objectForKey:@"学生信息"];
    NSLog(@"%@",dic);
}
- (IBAction)save2:(id)sender {
    NSDictionary *dic = @{@"姓名":self.nameTextField.text,@"年龄":self.ageTextField.text,@"学号":self.idTextField.text};
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    [dic writeToFile:filePath atomically:YES];
}
- (IBAction)read2:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.plist"];
    NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
    NSLog(@"%@",dic);
}
- (IBAction)save3:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    studentModel *m = [[studentModel alloc] init];
    m.stuID = self.idTextField.text;
    m.stuAge = self.ageTextField.text;
    m.stuName = self.nameTextField.text;
    NSMutableData *mData = [NSMutableData data];
    NSKeyedArchiver *arch = [[NSKeyedArchiver alloc] initForWritingWithMutableData:mData];
    [arch encodeObject:m forKey:@"Student"];
    [arch finishEncoding];
    
    [mData writeToFile:filePath atomically:YES];
}
- (IBAction)read3:(id)sender {
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"student.arch"];
    //读出数据
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    //创建解档对象
    NSKeyedUnarchiver *unArch = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    //从解档对象中解档Student
    studentModel *stu = [unArch decodeObjectForKey:@"Student"];
    [unArch finishDecoding];
    
    self.idTextField.text = stu.stuID;
    self.nameTextField.text = stu.stuName;
    self.ageTextField.text = stu.stuAge;
    NSLog(@"%@,%@,%@",self.idTextField.text,self.nameTextField.text,self.ageTextField.text);
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
